package com.jincheng.framework.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.jincheng.common.constant.AutoFillConstant;
import com.jincheng.common.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Date;

import static com.jincheng.common.utils.SecurityUtils.getUserId;
import static com.jincheng.common.utils.SecurityUtils.getUsername;

@Component
@Slf4j
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("开始插入填充插入时间、更新时间...");
        this.strictInsertFill(metaObject, AutoFillConstant.CREATE_BY, String.class, getUsername());
        this.strictInsertFill(metaObject, AutoFillConstant.CREATE_TIME, Date.class, DateUtils.getNowDate());
        this.strictInsertFill(metaObject, AutoFillConstant.UPDATE_BY, String.class, getUsername());
        this.strictInsertFill(metaObject, AutoFillConstant.UPDATE_TIME, Date.class, DateUtils.getNowDate());
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("开始更新填充更新时间...");
        this.strictInsertFill(metaObject, AutoFillConstant.UPDATE_BY, String.class, getUsername());
        this.strictUpdateFill(metaObject, AutoFillConstant.UPDATE_TIME, Date.class, DateUtils.getNowDate());
    }
}
